//
//  LoginVC.m
//  TST
//
//  Created by POP3mac on 2020/4/27.
//  Copyright © 2020 YTYanK. All rights reserved.
//

#import "LoginVC.h"
#import "MainVC.h"
#import "AppDelegate.h"

@interface LoginVC ()<UITextFieldDelegate>

@end

@implementation LoginVC


//- (UILabel *)logoTitle {
//        if (!_logoTitle) {
//            _logoTitle = [[UILabel alloc]init];
//            _logoTitle.text = @"TST";
////            _logoTitle.textColor = WhiteColor;
//            _logoTitle.font = [UIFont systemFontOfSize:72 weight:UIFontWeightBold];
//
//        }
//        return _logoTitle;
//}

//-(UILabel *)beInvLab{

//}


- (void)viewDidLoad {
    [super viewDidLoad];
    CGAffineTransform matrix = CGAffineTransformMake(1,
                                     0, tanf(-10 * (CGFloat)M_PI /
                                             180), 1,
                                     0, 0);
    self.logoTitle.transform = matrix;
    self.account.maxLength = 15;
    self.password.maxLength = 10;
    [self.account borderForColor:YTYRGBA(238, 238, 238, 1) borderWidth:1 borderType:YTYBorderSideTypeBottom];
    self.account.delegate = self;
    self.password.delegate = self;
    self.account.returnKeyType = UIReturnKeyNext;
    self.account.returnKeyType = UIReturnKeyDone;
    [self.password borderForColor:YTYRGBA(238, 238, 238, 1) border:UIEdgeInsetsMake(1, 1, 1, 1) borderType:YTYBorderSideTypeBottom];
    
    [self.login addTarget:self action:@selector(loginClick:) forControlEvents:UIControlEventTouchUpInside];
   UITapGestureRecognizer *tap =   [YTYTools obtainTapGestureRecognizerObjectWithAction:@selector(tapClick) withTarget:self];
    [self.view addGestureRecognizer:tap];
}
- (void)tapClick {
   [self.view endEditing:YES];
}

- (void)extracted:(NSMutableDictionary *)mutdic {
    [TSTApi requestWithUrl:AULogin parameters:mutdic method:NetMethodPOST returnModel:^(id  _Nonnull model) {
       
            if(model == nil && [model isEqual:[NSNull null]]) {
                [MBProgressHUD showError:@"該帳號沒有數據"];
                [NSUD setBool:false forKey:@"isLogin"];
            }else {
                [NSUD setBool:true forKey:@"isLogin"];
              
                [[YYCache cacheWithName:AULogin] setObject:model forKey:@"UserModel"];
                [TSTApi sharedTSTApi].userModel = [UserModel yy_modelWithDictionary:model];
                
                [AppDelegate login];
                [MBProgressHUD showSuccess:@"登錄成功"];
            }

        
        
        
    } returnError:^(id  _Nonnull error) {
        [MBProgressHUD showError:error];
        
    }];
}

- (void)loginClick:(UIButton *)sender {
//    self.account.text = @"jasonmak";
//    self.password.text = @"123456";
    
    if (self.account.text.length < 2 ||  self.password.text.length < 2 ) {
        [MBProgressHUD showError:@"帳戶密碼錯誤，請重新輸入!"];
        return;
    }
    
    // --123456
    NSMutableDictionary *mutdic = [NSMutableDictionary dictionary];
    [mutdic setObject:self.account.text forKey:@"username"];
    [mutdic setObject:self.password.text forKey:@"password"];
    [self extracted:mutdic];
    
    
    
   
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == self.account)
    {
        [textField becomeFirstResponder];
    }else if (textField == self.password){
//        loginClick:(UIButton *)sender
        [self loginClick:self.login];
    }
    return YES;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
